<?php

namespace App\Admin\Repositories;

use App\Models\OrgDepartment as Model;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Support\Facades\DB;
use App\Models\OrgDepartmentOwner as OwnerModel;

class OrgDepartment extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;
    protected $ownerModel = OwnerModel::class;

    public function getById($id)
    {
        $ret = $this->eloquentClass::query()->where('department_id', $id)->first();

        if ($ret) {
            return $ret->toArray();
        } else {
            return [];
        }
    }

    public function getOptionList()
    {
        $cityList = $this->eloquentClass::query()->select(['department_id', 'name'])->get()->toArray();
        $options = [];
        foreach ($cityList as $key => $val) {
            $options[$val['department_id']] = $val['name'];
        }

        return $options;
    }

    public function getOptionListByDepartmentIds(array $departmentIds)
    {
        $cityList = $this->eloquentClass::query()
            ->whereIn('department_id', $departmentIds)
            ->select(['department_id', 'name'])
            ->get()
            ->toArray();
        $options = [];
        foreach ($cityList as $key => $val) {
            $options[$val['department_id']] = $val['name'];
        }

        return $options;
    }

    public function add(string $name, array $uidList) : bool
    {
        DB::beginTransaction();
        $id = $this->eloquentClass::query()->insertGetId(['name' => $name]);
        if ($id) {
            $data = [];
            foreach ($uidList as $uid){
                if (!empty($uid))
                    $data[] = ['department_id' => $id, 'owner_uid' => $uid];
            }
            if ($this->ownerModel::query()->insert($data)){
                DB::commit();
                return true;
            }
        }
        DB::rollBack();
        return false;
    }

    public function updt(int $departmentId, string $name, array $uidList)
    {
        DB::beginTransaction();
        $id = $this->eloquentClass::query()
            ->where('department_id', $departmentId)
            ->update(['name' => $name]);

        $this->ownerModel::query()->where('department_id', $departmentId)->delete();

        if ($id) {
            $data = [];
            foreach ($uidList as $uid){
                if (!empty($uid))
                    $data[] = ['department_id' => $departmentId, 'owner_uid' => $uid];
            }
            if ($this->ownerModel::query()->insert($data)){
                DB::commit();
                return true;
            }
        }
        DB::rollBack();
        return false;
    }

    public function del(int $id) : bool
    {
        DB::beginTransaction();
        if (
            $this->eloquentClass::query()->where('department_id', $id)->delete() &&
            $this->ownerModel::query()->where('department_id', $id)->delete()
        ){
            DB::commit();
            return true;
        }
        DB::rollBack();
        return false;
    }
}
